<?php

/* 

Script for populating custom column in the node table 

Prerequisite is to create column using followin SQL commands:

ALTER TABLE `node` ADD `last_comment` INT( 11 ) NOT NULL DEFAULT '0';
ALTER TABLE `node` ADD INDEX ( `last_comment` ) ;

*/

include_once './includes/bootstrap.inc';
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);

// Getting either node changed timestamp or last comment timestamp, weither is greater

$query = db_query("SELECT node.nid, GREATEST(node.changed, node_comment_statistics.last_comment_timestamp) AS last_comment FROM node LEFT JOIN node_comment_statistics ON node.nid = node_comment_statistics.nid");

// Populating custom field with value above

while ($node = db_fetch_object($query)) {
  db_query("UPDATE {node} SET last_comment = %d WHERE nid = %d", $node->last_comment, $node->nid);
  print $node->last_comment. ' ' .$node->nid. '<br />'; // debug output
}
